<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN">
<html>
<head>
<title>HowTo</title>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
    <meta name="generator" content="Doc-O-Matic" />
    <meta http-equiv="Content-Style-Type" content="text/css" />
    <link rel="STYLESHEET" href="default.css" type="text/css" />

<script type="text/javascript" src="scripts.js"></script>
</head>
<body class="Element700" onload="onBodyLoadEx('frames.html', 'topic', 'HowTo.html');" onmousedown="onBodyMouseDown();">

<!-- Begin Popups -->

<!-- End Popups -->

<!-- Begin Page Header -->
<div class="Element710" id="areafixed">
<div class="Element94">
<em>You are here:</em> <a href="HowTo.html" target="topic">HowTo</a></div>
<div class="Element92">
<table width="100%" cellspacing="0" cellpadding="0">
<tr><td width="50%">
<div class="Element2">
<a href="contents.html" target="tocidx"><img src="btn_globals_contents_midblue.gif" border="0" alt="Contents" title="Contents" onmouseover="switchImage(this, 'btn_globals_contents_midblue_hover.gif');" onmouseout="switchImage(this, 'btn_globals_contents_midblue.gif');"></a><a href="idx.html" target="tocidx"><img src="btn_globals_index_midblue.gif" border="0" alt="Index" title="Index" onmouseover="switchImage(this, 'btn_globals_index_midblue_hover.gif');" onmouseout="switchImage(this, 'btn_globals_index_midblue.gif');"></a><a href="index.html" target="topic"><img src="btn_globals_home_midblue.gif" border="0" alt="Home" title="Home" onmouseover="switchImage(this, 'btn_globals_home_midblue_hover.gif');" onmouseout="switchImage(this, 'btn_globals_home_midblue.gif');"></a></div>
</td><td width="50%">
<div class="Element90">
<a href="Installation.html" target="topic"><img src="btn_prev_midblue.gif" border="0" alt="Previous" title="Previous" onmouseover="switchImage(this, 'btn_prev_midblue_hover.gif');" onmouseout="switchImage(this, 'btn_prev_midblue.gif');"></a><img src="btn_up_d.gif" border="0" alt="Up" title="Up"><a href="Version_history.html" target="topic"><img src="btn_next_midblue.gif" border="0" alt="Next" title="Next" onmouseover="switchImage(this, 'btn_next_midblue_hover.gif');" onmouseout="switchImage(this, 'btn_next_midblue.gif');"></a></div>
</td></tr></table><div class="Element5">
HowTo</div>
</div>
</div>

<!-- End Page Header -->

<!-- Begin Client Area -->
<div class="Element720" id="areascroll">
<div class="Element721">

<!-- Begin Page Content -->
<div class="Element58">
<a name="Description"></a><div class="Element11">
<div class="Element10">
<div class="Element15">
How general sql parser works:</div>
<p class="Element10">
&nbsp;</p>
<p class="Element10">
1. Read sql text, and it will be tokenlized by lex parser into a list of tokens. OnTokenlizerToken event is fired when a lex token is found. user can modify the source token in the event as they needed. After read all input sql, a source token list is generated which is the input of the yacc parser. AfterTokenlizer event is also fired before the source token list parsed by yacc parser.&nbsp;</p>
<p class="Element10">
&nbsp;</p>
<p class="Element10">
2. Yacc parser read source token in SourceTokenList,based on the BNF of different database dialects, the parser will create a raw parse tree if no syntax error was found. During the parsing process, OnParserToken event is fired when a token is read. If there is a syntax error after reading a source token, then OnParserTokenError event is fired , you can create your own event handler to recover syntax error dynamically in this event.&nbsp;</p>
<p class="Element10">
&nbsp;</p>
<p class="Element10">
3.Translate the raw parse tree into a formal parse tree, top level node of the formal parse tree is type of sql statement such as <a href="TSelectSqlStatement.html" target="topic">TSelectSqlStatement</a>, <a href="TInsertSqlStatement.html" target="topic">TInsertSqlStatement</a>, <a href="TDeleteSqlStatement.html" target="topic">TDeleteSqlStatement</a>, <a href="TUpdateSqlStatement.html" target="topic">TUpdateSqlStatement</a>, <a href="TCreateTableSqlStatement.html" target="topic">TCreateTableSqlStatement</a> etc. which in turn includes other sub tree nodes(All available parse tree nodes). During the translation, parser will iterate the whole raw parse tree, and OnTableToken event will be fired if a source token stands for a table is found, OnFieldToken event will be fired if a source token stands for a field is found.&nbsp;</p>
<p class="Element10">
&nbsp;</p>
<p class="Element10">
4. How to use this formal parse tree dependeds on your requirement and imagination.&nbsp;</p>
<p class="Element10">
Getting information such as tables and fields in a sql statement is very easy, they are ready for use along with other db ojects in sql objects.&nbsp;</p>
<p class="Element10">
Visitors descends from TLzVisitorAbs can be used to iterate the parse tree. TLzParseTree2XMLVisitor is a simple visitor which tranlate the parse tree to XML output. This class is a good reference before you begin to write your own visitor to handle the parse tree.&nbsp;</p>
<p class="Element10">
Check the Visitor pattern used in General SQL Parser for more information on how to use visitor to handle the parse tree.&nbsp;</p>
<p class="Element10">
&nbsp;</p>
<p class="Element10">
Followings are some typical usages of parse tree.&nbsp;</p>
<p class="Element10">
&nbsp;</p>
<p class="Element10">
<img src="workflow.gif" border="0" alt="" title="">&nbsp;</p><div class="Element15">
Pretty print function</div>
<p class="Element10">
Generate pretty sql from parse tree. since pretty sql is built from parse tree, the input sql must be parsed first before print out pretty sql, and there should be no syntax errors in sql text. If there are syntax errors, no parse tree will be built, so can't generate pretty sql by gsqlparser.&nbsp;</p>
<p class="Element10">
&nbsp;</p><div class="Element15">
Generate sql dynamically</div>
<p class="Element10">
Change objects in parse tree such as field, table and then regenereate sql on the fly.&nbsp;</p>
<p class="Element10">
The other way to generate sql is creating sql objects and then build parse tree manually, such as create a <a href="TSelectSqlStatement.html" target="topic">TSelectSqlStatement</a>, TField, TTable object, and then assign TField and TTable objects to <a href="TSelectSqlStatement.html" target="topic">TSelectSqlStatement</a> , thus a parse tree will be created with <a href="TSelectSqlStatement.html" target="topic">TSelectSqlStatement</a> as root node. From this parse tree, you can generate sql text.&nbsp;</p>
<p class="Element10">
&nbsp;</p><div class="Element15">
Sql translate between different database dialects</div>
<p class="Element10">
Sql translate between different database dialects is really a challenge work , based on parse tree generated by gsqlparser, it is possible to do this job. But frankly speaking, the parse tree is just a place to start, it is your job to achieve this.</p></div>
</div>
</div>
<!-- End Page Content -->

<!-- Begin Page Footer -->
<div class="Element93">
<table width="100%" cellspacing="0" cellpadding="0">
<tr><td width="100%">
<div class="Element3">
Gudu software http://www.sqlparser.com</div>
</td></tr><tr><td width="100%">
<div class="Element97">
Send <a href="#" onclick="sendFeedback('info@sqlparser.com', 'Documentation Feedback', 'Project: General SQL Parser Help file%0ATopic ID: HowTo%0ATitle: HowTo');">comments</a> about this topic.</div>
</td></tr></table></div>

<!-- End Page Footer -->
</div>
</div>

<!-- End Client Area -->
</body></html>